﻿#region File Info Header
/*________________________________________________________________________________________

  Copyright (C) 2011 Jason Zhang, eagleboost@msn.com

  * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
  * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
  * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.

________________________________________________________________________________________*/
#endregion File Info Header

namespace EagleBoost.Wpf.Presentation.Controls.ColorPicker
{
  using System;
  using System.Reflection;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Media;

  /// <summary>
  /// ColorPickerComboBox
  /// </summary>
  [TemplatePart(Name = PartColorPickerControl, Type = typeof(ColorPickerControl))]
  public class ColorPickerComboBox : ComboBox
  {
    #region Constants
    private const string PartColorPickerControl = "PART_ColorPickerControl";
    private const string ControlXamlFile = "component/Controls/ColorPicker/ColorPickerComboBox.xaml";
    #endregion Constants

    #region Declarations
    private static readonly Style ControlStyle;
    #endregion Declarations

    #region ctors
    static ColorPickerComboBox()
    {
      var assembly = System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().ManifestModule.Name);
      var rd = Application.LoadComponent(new Uri(string.Format("{0};{1}", assembly, ControlXamlFile), UriKind.Relative)) as ResourceDictionary;
      if (rd != null)
      {
        ControlStyle = rd["ColorPickerComboBox"] as Style;
      }

      StyleProperty.OverrideMetadata(typeof(ColorPickerComboBox), new FrameworkPropertyMetadata(ControlStyle));
      DefaultStyleKeyProperty.OverrideMetadata(typeof(ColorPickerComboBox), new FrameworkPropertyMetadata(typeof(ColorPickerComboBox)));
    }
    #endregion ctors

    #region Dependency Properties
    public static readonly DependencyProperty SelectedColorProperty =
      DependencyProperty.Register("SelectedColor", typeof(Color?), typeof(ColorPickerComboBox));

    /// <summary>
    /// Selected Color
    /// </summary>
    public Color? SelectedColor
    {
      get { return (Color?)ColorPickerControl.GetValue(ColorPickerControl.SelectedColorProperty); }
      set { ColorPickerControl.SetValue(ColorPickerControl.SelectedColorProperty, value); }
    }

    public static readonly DependencyProperty SelectedBrushProperty =
      DependencyProperty.Register("SelectedBrush", typeof(Brush), typeof(ColorPickerComboBox),
      new PropertyMetadata(null));

    /// <summary>
    /// Selected Brush
    /// </summary>
    public Brush SelectedBrush
    {
      get { return (Brush)ColorPickerControl.GetValue(ColorPickerControl.SelectedBrushProperty); }
      set { ColorPickerControl.SetValue(ColorPickerControl.SelectedBrushProperty, value); }
    }
    #endregion Dependency Properties

    #region Public Properties
    public ColorPickerControl ColorPickerControl
    {
      get { return Template.FindName(PartColorPickerControl, this) as ColorPickerControl; }
    }
    #endregion Public Properties

    #region Overrides
    public override void OnApplyTemplate()
    {
      base.OnApplyTemplate();
      ItemsSource = ColorPickerControl.AvailableColors;
    }
    #endregion Overrides
  }
}
